<template>
  <div class="login">
    <h1 class="normal center title">账号登录</h1>
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="0px"
      style="width:300px;margin-top:40px;margin-left:22px"
    >
      <el-form-item prop="userName">
        <el-input v-model="form.username" placeholder="请输入账号" prefix-icon="iconfont icon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
          prefix-icon="iconfont icon-mima"
        ></el-input>
      </el-form-item>
      <el-button type="primary" style="width:100%" :loading="loading" @click.native="submitForm">登录</el-button>
    </el-form>
  </div>
</template>
<script>
import { login } from "@/api/login";
import { setSession, getSession } from "@/util/auth";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
      loading: false,
      redirect: "",
    };
  },
  created() {
    this.redirect = this.$route.query.redirect;
  },
  methods: {
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          login(this.form)
            .then((res) => {
              this.loading = false;
              if (res.code == 1) {
                setSession(res.token);
                this.$message({
                  showClose: true,
                  message: "登录成功",
                  type: "success",
                });
                if (this.redirect) {
                  this.$router.replace(this.redirect);
                } else {
                  this.$router.replace("/index");
                }
              } else if (res.code == 402) {
                this.$message({
                  showClose: true,
                  message: res.message,
                  type: "error",
                });
              }
            })
            .catch((res) => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["form"].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  height: 360px;
  width: 460px;
  box-sizing: border-box;
  padding: 0 60px;
  background: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: #ccc 1px -1px 4px 0px;
  .title {
    line-height: 60px;
    font-size: 24px;
    color: rgb(0, 124, 220);
    border-bottom: 1px solid #ccc;
  }
}
</style>
